<template>
  <el-scrollbar :wrap-class="{ wrapClass: true }">
    <div class="app-container">
      <div class="filter-container" size="small">
        <el-input
          v-model="listQuery.name"
          size="small"
          :placeholder="$t('common.oemName')"
          style="width: 200px"
          class="filter-item"
          clearable
        />
        <el-select
          v-model="listQuery.state"
          size="small"
          :placeholder="$t('common.select', [$t('common.status')])"
          filterable
          clearable
          class="filter-item"
          style="width: 170px"
        >
          <el-option key="1" :label="$t('common.enable')" value="1" />
          <el-option key="2" :label="$t('common.disabled')" value="2" />
        </el-select>
        <el-button
          class="filter-item"
          size="small"
          icon="el-icon-search"
          type="primary"
          plain
          @click="search"
        >
          {{ $t('table.search') }}
        </el-button>
        <el-button
          class="filter-item"
          size="small"
          icon="el-icon-plus"
          type="primary"
          @click="add"
        >
          {{ $t('table.add') }}
        </el-button>
        <el-button
          size="small"
          class="filter-item right"
          icon="el-icon-refresh"
          type="primary"
          plain
          @click="refresh"
        >
          {{ $t('common.refresh') }}
        </el-button>
      </div>

      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        :height="
          listQuery.total > 0 ? 'calc(100vh - 205px)' : 'calc(100vh - 170px)'
        "
        :header-cell-style="{ background: '#F2F6FC' }"
        element-loading-text="Loading"
        fit
        border
        size="medium"
      >
        <el-table-column
          align="center"
          :label="$t('common.id')"
          width="50"
          type="index"
        />
        <el-table-column
          align="center"
          prop="id"
          :label="$t('common.oemId')"
          width="80"
        />
        <el-table-column
          align="center"
          prop="domain"
          :label="$t('common.domain')"
          width="250"
        >
          <template slot-scope="scope">
            <span
              ><a
                style="color: rgb(64, 158, 255)"
                :href="'http://' + scope.row.domain"
                target="_blank"
                >{{ scope.row.domain }}</a
              ></span
            >
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="name"
          :label="$t('common.oemName')"
          width="200"
        />

        <el-table-column
          :label="$t('system.topLeftText')"
          min-width="200px"
          align="center"
          show-overflow-tooltip
        >
          <el-table-column
            :label="$t('system.topLeftTextStyle')"
            min-width="200px"
            align="center"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              {{ scope.row.title }}
              <div class="divider">
                <el-divider />
              </div>
              <strong v-if="scope.row.style == 1" style="color: #67c23a">{{
                $t('system.oneline')
              }}</strong>
              <strong v-if="scope.row.style == 2" style="color: #409eff">{{
                $t('system.doublelines')
              }}</strong>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column :label="$t('common.status')" width="80" align="center">
          <template slot-scope="scope">
            <strong v-if="scope.row.state === 1" style="color: #67c23a">{{
              $t('common.enable')
            }}</strong>
            <strong v-else style="color: #f56c6c">{{
              $t('common.disabled')
            }}</strong>
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          :label="$t('common.createTime')"
          align="center"
          width="155px"
        >
          <el-table-column
            prop="province"
            :label="$t('common.updateTime')"
            align="center"
            width="155px"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.createdAt }}</span>
              <div class="divider">
                <el-divider />
              </div>
              <span>{{ scope.row.updatedAt }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column
          align="center"
          fixed="right"
          prop
          :label="$t('common.actions')"
          width="300"
        >
          <template slot-scope="scope">
            <el-button-group>
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="edit(scope.row.id)"
                >{{ $t('common.edit') }}
              </el-button>
              <el-button
                type="primary"
                size="mini"
                @click="editEmailSettings(scope.row.id)"
                >{{ $t('system.emailConfig') }}
              </el-button>
              <el-button
                type="success"
                size="mini"
                icon="el-icon-setting"
                @click="allotUser(scope.row)"
              >
                {{ $t('system.allotOem') }}
              </el-button>
              <el-button
                :disabled="scope.row.id == 1"
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="deleteRow(scope.row.id)"
                >{{ $t('common.delete') }}
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination
        v-show="listQuery.total > 0"
        :total="listQuery.total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="fetchData"
      />

      <edit-page :id="id" :show.sync="showState" @closeAction="closeAction" />
      <edit-email-settings
        :id="id"
        :show.sync="editEmailSettingsState"
        @closeAction="closeAction"
      />
      <allot-page :show.sync="showAllotState" :oemId="id" :name="name" />
    </div>
  </el-scrollbar>
</template>
<script>
import Pagination from '@/components/Pagination'
import EditPage from '@/views/systemManage/oem/edit'
import AllotPage from '@/views/systemManage/oem/allot'
import { getAction, postAction } from '@/api/common'
import EditEmailSettings from './editEmailSettings.vue'

export default {
  name: 'index',
  components: {
    Pagination,
    EditPage,
    AllotPage,
    EditEmailSettings,
  },
  data() {
    return {
      listLoading: true,
      list: [],
      // 列表搜索条件
      listQuery: {
        page: 1,
        total: 0,
        limit: 20,
        name: '',
        state: '',
      },
      id: '',
      name: '',
      showState: false,
      showAllotState: false,
      editEmailSettingsState: false,
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      this.listLoading = true
      const { data } = await getAction(this.listQuery, 'oem/GetList')
      this.list = data.items
      this.listQuery.total = data.total
      this.listLoading = false
    },
    add() {
      this.id = ''
      this.showState = true
    },
    edit(id) {
      this.id = id
      this.showState = true
    },
    editEmailSettings(id) {
      this.id = id
      this.editEmailSettingsState = true
    },
    allotUser(row) {
      this.id = row.id
      this.name = row.name
      this.showAllotState = true
    },
    async deleteRow(id) {
      this.$confirm(this.$t('common.confirmDelete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning',
      })
        .then(async () => {
          await postAction({ id: id }, 'oem/delInfo')
          this.$notify({
            title: this.$t('common.success'),
            message: this.$t('common.operationSuccess'),
            type: 'success',
            duration: 2000,
          })
          this.refresh()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: this.$t('common.cancelOperation'),
          })
        })
    },
    search() {
      this.listQuery.page = 1
      this.fetchData()
    },
    refresh() {
      this.fetchData()
    },
    closeAction() {
      this.showState = false
      this.editEmailSettingsState = false
      this.fetchData()
    },
  },
}
</script>
<style scoped>
.divider /deep/ .el-divider--horizontal {
  margin: 10px 0;
}
</style>
